package com.scanner.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;

import com.scanner.hardware.R;

/**
 * Created by UglyB
 * 0n 2019/6/21
 */
public class SelectItemsDialog {

    private OnAlterDialogPositiveClickListener listener;
    private Context context;

    public SelectItemsDialog(Context context,String title,String[] datas,int select,OnAlterDialogPositiveClickListener listener){
        this.context = context;
        this.listener = listener;
        showSingleAlertDialog(title,datas,select);
    }


    private AlertDialog alertDialog; //单选框

    int pos = 0;

    private void showSingleAlertDialog(String title,String[] datas,int select) {
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
        alertBuilder.setTitle(title);
        alertBuilder.setSingleChoiceItems(datas, select, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                pos = i;
            }
        });

        alertBuilder.setPositiveButton(context.getString(R.string.str_ensure), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                alertDialog.dismiss();
                if(listener != null){
                   listener.onSure(pos);
                }
            }
        });

        alertBuilder.setNegativeButton(context.getString(R.string.cancel), (dialogInterface, i) -> alertDialog.dismiss());

        alertDialog = alertBuilder.create();
        alertDialog.show();
    }


    public interface OnAlterDialogPositiveClickListener{
        void onSure(int position);
    }

}
